package org.example;

import org.example.Entity.Score;
import org.example.Entity.Student;
import org.example.controller.GradeController;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class Main {
    public static void main(String[] args) throws IOException {
//        //注册账号    需要传递角色是老师还是学生
//        System.out.println("请输入学生学号：");
//        Scanner scanner = new Scanner(System.in);
//        String course = scanner.nextLine();
//        System.out.println("请输入学生平时成绩：");
//        double usually = scanner.nextDouble();
//        System.out.println("请输入学生考试成绩：");
//        double examination = scanner.nextDouble();
//        System.out.println("请输入课程id：");
//        int ids = scanner.nextInt();
        GradeController gradeController = new GradeController();
//        int inserted = gradeController.insertScore(course, ids, usually, examination);
//        if (inserted > 0) {
//            System.out.println("插入成功");
//        }else {
//            System.out.println("插入失败");
//        }
        //全部
        List<Student> students = gradeController.selectByGradeAll();
        //分页
        List<Student> students1 = gradeController.selectByGradeAll1(1, 2);
        //提取数据
        DefaultTableModel model = new DefaultTableModel();
        JTable table = new JTable(model);
        //        居中
        table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean cellHasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, cellHasFocus, row, column);
                setHorizontalAlignment(SwingConstants.CENTER);
                return this;
            }
        });
        //先创建一个空列表
        ArrayList<Object> objects = new ArrayList<>();
        JFrame frame = new JFrame("Database Table Example");
        model.addColumn("名字");
        model.addColumn("学号");
        model.addColumn("课程");
        model.addColumn("成绩");
        //使用增强型for循环循环查询到的对象 先循环主对象
        for (Student student : students) {
            //然后从对象中抽出子对象进行循环提取数据
            List<Score> scores = student.getScores();
            for (Score score : scores) {
                //创建一个Obiect类型的数组，用于接受从不同实体类方法中循环出来的所需数据追加为一个一维数组
                Object[] row = {student.getName(),student.getStudentId(),score.getCourse().getCourseName(), String.valueOf(score.getScores())};
                //将一个一维数组追加到创建好的列表中
                objects.add(row);
            }
        }
        //执行完上面的代码后得到一个不同学生的成绩数组，将其循环出来添加到表格上
        for (Object row : objects) {
            model.addRow((Object[]) row);
        }



//        HashMap<String, Object> map = gradeController.selectByGradeAll();
//        System.out.println("数据："+map.get("list"));
//        System.out.println("总数："+map.get("total"));
//        System.out.println("一页多少条："+map.get("pageSize"));
//        System.out.println("第几页："+map.get("pageNumber"));
//        System.out.println(map);
//        List list = (List) map.get("list");
        // 创建JTable并设置模型



        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);

        model.setRowCount(0);
    }

}